package com.zjg.servlet;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.zjg.bean.User;
import com.zjg.service.UserServiceImp;
import com.zjg.service.iface.UserService;

/**
 * Servlet implementation class UserMessageAdmin
 */
@WebServlet("/usermessageadmin.servlet")
public class UserMessageAdmin extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = response.getWriter();
		String param = request.getParameter("param");
		String ids = request.getParameter("id");
		String EditId=request.getParameter("editid");
		String fid=request.getParameter("fid");
		if(Objects.equals("addfriend", param)) {
			UserService userservice=new UserServiceImp();
			userservice.addFriend(Integer.valueOf(ids), Integer.valueOf(fid));
		}
		if(Objects.equals("getUserById", param)) {
			UserService userservice=new UserServiceImp();
			userservice.getUserById(Integer.valueOf(ids));
		}
		if(Objects.equals("getIcon", param)) {
			int id=Integer.valueOf(ids);
			UserService users=new UserServiceImp();
			BufferedImage image = null;
		    image = ImageIO.read(users.getHeadIcon(id));
		    response.reset();
		    ImageIO.write(image,"JPEG", response.getOutputStream());
	        response.getOutputStream().flush();
	        response.getOutputStream().close();
		}
		if (Objects.equals("get", param)) {
			UserService us = new UserServiceImp();
			List<User> list = us.getUserMessage();
			list.stream().forEach((item) -> {
				us.setUserHobby(item);
			});
			Map map = new HashMap();
			map.put("userMessage", list);
			String json = JSONObject.toJSONString(map);

			out.println(json);
			out.flush();
			out.close();
		}
		if (Objects.equals("delete", param)) {

			UserService us = new UserServiceImp();
			Map map = new HashMap();

			if (us.deleteUsers(ids)) {
				map.put("msg", "ok");
			} else {
				map.put("msg", "l");

			}
			;
			String json = JSONObject.toJSONString(map);
			out.println(json);
			out.flush();
			out.close();
		}
		if (Objects.equals("EditUser", param)) {
			User user=new User();
			UserService us = new UserServiceImp();
			List<User> list = us.getUserMessage();
			list.stream().forEach((item) -> {
				us.setUserHobby(item);
			});

			list.stream().forEach((item) -> {
				if(item.getId()==Integer.parseInt(EditId)) {
					user.setId(item.getId());
					user.setUserName(item.getUserName());
					user.setPassword(item.getPassword());
					user.setEmail(item.getEmail());
					user.setHobby(item.getHobby());
					user.setHobbyName(item.getHobbyName());
					user.setPlace(item.getPlace());
					user.setPlaceName(item.getPlaceName());
					user.setProvinceName(item.getProvinceName());
					user.setSex(item.getSex());
					
				}
			});
			Map map=new HashMap();
			map.put("date",user);
			String json = JSONObject.toJSONString(map);
			out.println(json);
			out.flush();
			out.close();
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doGet(request, response);
	}

}
